package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateTimetable
//@description: 创建Timetable记录
//@param: timetable model.Timetable
//@return: err error

func CreateTimetable(timetable model.Timetable) (err error) {
	err = global.GVA_DB.Create(&timetable).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteTimetable
//@description: 删除Timetable记录
//@param: timetable model.Timetable
//@return: err error

func DeleteTimetable(timetable model.Timetable) (err error) {
	err = global.GVA_DB.Delete(&timetable).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteTimetableByIds
//@description: 批量删除Timetable记录
//@param: ids request.IdsReq
//@return: err error

func DeleteTimetableByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Timetable{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateTimetable
//@description: 更新Timetable记录
//@param: timetable *model.Timetable
//@return: err error

func UpdateTimetable(timetable model.Timetable) (err error) {
	err = global.GVA_DB.Save(&timetable).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetTimetable
//@description: 根据id获取Timetable记录
//@param: id uint
//@return: err error, timetable model.Timetable

func GetTimetable(id uint) (err error, timetable model.Timetable) {
	err = global.GVA_DB.Where("id = ?", id).First(&timetable).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetTimetableInfoList
//@description: 分页获取Timetable记录
//@param: info request.TimetableSearch
//@return: err error, list interface{}, total int64

func GetTimetableInfoList(info request.TimetableSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Timetable{})
    var timetables []model.Timetable
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&timetables).Error
	return err, timetables, total
}

//@author: [buff]()
//@function: GetTimetableByTeacherName
//@description: 根据获取TeacherNameTimetable记录
//@param: TeacherName string
//@return: err error, timetable model.Timetable

func GetTimetableByTeacherName(TeacherName string) (err error, list interface{}) {
	var timetables []model.Timetable
	err = global.GVA_DB.Where("TeacherName = ?", TeacherName).Find(&timetables).Error
	return err, timetables
}

//@author: [buff]()
//@function: GetTimetableByTeacherName
//@description: 根据获取TeacherNameTimetable记录
//@param: ClassId string
//@return: err error, timetable model.Timetable

func GetTimetableByClassId(ClassId string) (err error, list interface{}) {
	var timetables []model.Timetable
	err = global.GVA_DB.Where("ClassId = ?", ClassId).Find(&timetables).Error
	return err, timetables
}
